package cn.wx.order.controller;


import cn.wx.basic.annotation.PreAuthorize;
import cn.wx.basic.util.GetConTest;
import cn.wx.basic.util.JsonResult;
import cn.wx.basic.util.PageList;

import cn.wx.order.Query.OrderPetAcquisitionQuery;
import cn.wx.order.domain.OrderPetAcquisition;
import cn.wx.order.dto.ConfirmDto;
import cn.wx.order.service.IOrderPetAcquisitionService;
import cn.wx.org.domain.Employee;
import cn.wx.org.domain.Shop;
import cn.wx.org.mapper.EmployeeMapper;
import cn.wx.org.mapper.ShopMapper;
import cn.wx.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderPetAcquisition")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class OrderPetAcquisitionController {

    @Autowired
    private IOrderPetAcquisitionService iOrderPetAcquisitionService;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    //使用get方式请求  查询所有
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<OrderPetAcquisition> findAll(){
        return iOrderPetAcquisitionService.loadAll();
    }

    @GetMapping("/{id}")   //获取get请求里面的参数   查询一个
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "单独查询部门",sn = "orderPetAcquisition:get") //value后面用来做权限校验
    public OrderPetAcquisition findById(@PathVariable("id") Long id ){
        return iOrderPetAcquisitionService.loadById(id);
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID取消订单" )
    public JsonResult cancle(@PathVariable("id") Long id){
        try {
            iOrderPetAcquisitionService.cancle(id);
           return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


    //分页
    @PostMapping      //使用post方式请求
    @ApiOperation(value = "分页查询" )
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request){

        try {
            Logininfo logininfo = GetConTest.getLogininfo(request);
            Long id = logininfo.getId();
            Employee employee = employeeMapper.loadByLogininfoId(id);
            if(employee.getShop_id()!=null){  //表示普通店铺
                Shop shop = shopMapper.loadById(employee.getShop_id());
                if(shop.getAdmin_id()!=employee.getId()){
                    query.setEmpId(employee.getId());
                }
                // 店铺人员
                query.setShopId(employee.getShop_id());
            }
            return  iOrderPetAcquisitionService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }


    @PostMapping("/confirm")
    public JsonResult confirm(@RequestBody ConfirmDto dto){
        try {
            iOrderPetAcquisitionService.ConfirmDto(dto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常");
        }
    }


}
